package com.iexbuy.ihg.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.iexbuy.ihg.R;
import com.iexbuy.ihg.adapter.AddressListAdapter;
import com.iexbuy.ihg.adapter.DealHistoryDetailListAdapter;
import com.iexbuy.ihg.common.CommUtil;
import com.iexbuy.ihg.common.ExtraKeyConstant;
import com.iexbuy.ihg.entity.AddressObject;
import com.iexbuy.ihg.entity.AddressObjectResponse;
import com.iexbuy.ihg.entity.DealHistoryDetailListObjectResponce;
import com.iexbuy.ihg.entity.DealHistoryDetailObject;
import com.iexbuy.ihg.net.ServiceHelper;
import com.iexbuy.ihg.utils.JsonUtils;
import com.iexbuy.ihg.utils.ToastUtils;
import com.jingchen.pulltorefresh.PullToRefreshLayout;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class WeDeAddresssFragment extends BaseFragment {

	private static WeDeAddresssFragment homeFragmentInstent;

	@ViewInject(R.id.msgListLayout)
	private PullToRefreshLayout msgListRefreshLayout;
	@ViewInject(R.id.listView)
	private ListView listView;


	private List<AddressObject> addressObjects = new ArrayList<AddressObject>();
	private int pageNo = 0;
	private AddressListAdapter addressListAdapter;

	private static OnSelectedListener onSelectedListener = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		homeFragmentInstent = this;
	}

	@Override
	protected View _onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_wede_address, container, false);
		ViewUtils.inject(this, rootView);
		initView(rootView);
		return rootView;
	}

	private void initView(View rootView) {
		mBaseActivity.setTitle(getResources().getString(R.string.we_de_address));
		mBaseActivity.setTitleLayoutVisibility(true);
		mBaseActivity.setHomeActivity(false);

		msgListRefreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
			@Override
			public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
				initAddressListData();
			}

			@Override
			public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {}
		});
	}

	public void onResume() {
		super.onResume();
		msgListRefreshLayout.autoRefresh();
	}


	public void initAddressListData() {

		HashMap<String, String> paramMap = new HashMap<String, String>();
		ServiceHelper.send(mBaseActivity, ServiceHelper.GET_MY_ADDRESS, paramMap,
				new RequestCallBack<String>() {
					@Override
					public void onStart() {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.REFRESHING);
					}

					@Override
					public void onCancelled() {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.DONE);
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
						AddressObjectResponse rep = JsonUtils.parseJsonStrToObject(responseInfo.result, AddressObjectResponse.class);
						addressObjects.clear();
						addressObjects.add(new AddressObject("增加收货地址", "-1"));
						addressObjects.addAll(rep.getBody());
						initdealHistoryDetailObjectView();
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
						ToastUtils.toast(mBaseActivity, msg);
					}
				});
	}

	private void initdealHistoryDetailObjectView() {
		if (addressListAdapter == null) {
			addressListAdapter = new AddressListAdapter(mBaseActivity, addressObjects);
			listView.setAdapter(addressListAdapter);
		}
		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
				if (onSelectedListener != null && !"-1".equalsIgnoreCase(addressObjects.get(i).getAddressId())) {
					onSelectedListener.onSelected(addressObjects.get(i));
					onSelectedListener = null;
					mBaseActivity.finish();
				} else {
					WeDeAddresssDetailFragment.setAddressObject(addressObjects.get(i));
					CommUtil.openSingleFragmentActivity(mBaseActivity, WeDeAddresssDetailFragment.class);
				}
			}
		});
		addressListAdapter.notifyDataSetChanged();
	}

	public static void setOnSelectedListener(OnSelectedListener onSelectedListener) {
		WeDeAddresssFragment.onSelectedListener = onSelectedListener;
	}

	/**
	 * @return the homeFragmentInstent
	 */
	public static WeDeAddresssFragment getHomeFragmentInstent() {
		return homeFragmentInstent;
	}

	public interface OnSelectedListener{
		public void onSelected(AddressObject addressObject);
	}

}
